package services

import (
	"strconv"
	"time"

	"github.com/mlogclub/simple/sqls"
	"github.com/mlogclub/simple/web/params"
	"gorm.io/gorm"

	"bbs-go/internal/models"
	"bbs-go/internal/repositories"
)

var ChatGroupsMessagesService = newChatGroupsMessagesService()

func newChatGroupsMessagesService() *chatGroupsMessagesService {
	return &chatGroupsMessagesService{}
}

type chatGroupsMessagesService struct {
}

func (s *chatGroupsMessagesService) Get(id int64) *models.ChatGroupsMessages {
	return repositories.ChatGroupsMessagesRepository.Get(sqls.DB(), id)
}

func (s *chatGroupsMessagesService) Take(where ...interface{}) *models.ChatGroupsMessages {
	return repositories.ChatGroupsMessagesRepository.Take(sqls.DB(), where...)
}

func (s *chatGroupsMessagesService) Find(cnd *sqls.Cnd) []models.ChatGroupsMessages {
	return repositories.ChatGroupsMessagesRepository.Find(sqls.DB(), cnd)
}

func (s *chatGroupsMessagesService) FindPageByParams(params *params.QueryParams) (list []models.ChatGroupsMessages, paging *sqls.Paging) {
	return repositories.ChatGroupsMessagesRepository.FindPageByParams(sqls.DB(), params)
}

func (s *chatGroupsMessagesService) FindPageByCnd(cnd *sqls.Cnd) (list []models.ChatGroupsMessages, paging *sqls.Paging) {
	return repositories.ChatGroupsMessagesRepository.FindPageByCnd(sqls.DB(), cnd)
}

func (s *chatGroupsMessagesService) Create(t *models.ChatGroupsMessages) error {
	return repositories.ChatGroupsMessagesRepository.Create(sqls.DB(), t)
}

func (s *chatGroupsMessagesService) Update(t *models.ChatGroupsMessages) error {
	return repositories.ChatGroupsMessagesRepository.Update(sqls.DB(), t)
}

func (s *chatGroupsMessagesService) Updates(id int64, columns map[string]interface{}) error {
	return repositories.ChatGroupsMessagesRepository.Updates(sqls.DB(), id, columns)
}

func (s *chatGroupsMessagesService) UpdateColumn(id int64, name string, value interface{}) error {
	return repositories.ChatGroupsMessagesRepository.UpdateColumn(sqls.DB(), id, name, value)
}

func (s *chatGroupsMessagesService) GetGroupMessageList(page *sqls.Paging, groupId int64) (models.ChatGroupsMessagesListResponse, error) {
	limit := page.Limit
	offset := page.Limit * (page.Page - 1)
	if limit == 0 {
		limit = 10
	}
	db := sqls.DB()
	querySql := `select cgm.*,
	su.nickname as send_user_nickname,su.avatar as send_user_avatar
	from chat_groups_messages cgm 
	left join t_user su on cgm.send_user_id = su.id 
	where 1=1 and cgm.deleted_at is null  `
	whereSql := ""
	whereVars := []interface{}{}

	whereSql += " and group_id = ?"
	whereVars = append(whereVars, groupId)

	countSql := `select count(1)
	from chat_groups_messages cgm  
	where 1=1 `

	var total int64
	err := db.Raw(countSql+whereSql, whereVars...).Scan(&total).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return models.ChatGroupsMessagesListResponse{}, err
	}

	list := []models.ChatGroupsMessagesResponse{}
	orderBySql := " order by cgm.id desc LIMIT " + strconv.Itoa(limit) + " OFFSET " + strconv.Itoa(offset)
	err = db.Raw(querySql+whereSql+orderBySql, whereVars...).Find(&list).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return models.ChatGroupsMessagesListResponse{}, err
	}

	return models.ChatGroupsMessagesListResponse{
		List: list,
		Paging: &sqls.Paging{
			Page:  page.Page,
			Limit: page.Limit,
			Total: total,
		},
	}, nil
}

func (s *chatGroupsMessagesService) GetRecentGroupMessageList(groupId int64) (models.ChatGroupsMessagesListResponse, error) {
	limit := 30
	offset := 0
	db := sqls.DB()
	querySql := `select cgm.*,
	su.nickname as send_user_nickname,su.avatar as send_user_avatar
	from chat_groups_messages cgm 
	left join t_user su on cgm.send_user_id = su.id 
	where 1=1 and cgm.deleted_at is null  `
	whereSql := ""
	whereVars := []interface{}{}

	whereSql += " and cgm.created_at > ? "
	whereVars = append(whereVars, time.Now().AddDate(0, 0, -4).Format("2006-01-02 15:04:05"))

	whereSql += " and cgm.group_id = ?"
	whereVars = append(whereVars, groupId)

	countSql := `select count(1)
	from chat_groups_messages cgm  
	where 1=1 `

	var total int64
	err := db.Raw(countSql+whereSql, whereVars...).Scan(&total).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return models.ChatGroupsMessagesListResponse{}, err
	}

	list := []models.ChatGroupsMessagesResponse{}
	orderBySql := " order by cgm.id desc LIMIT " + strconv.Itoa(limit) + " OFFSET " + strconv.Itoa(offset)
	err = db.Raw(querySql+whereSql+orderBySql, whereVars...).Find(&list).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return models.ChatGroupsMessagesListResponse{}, err
	}

	return models.ChatGroupsMessagesListResponse{
		List: list,
		Paging: &sqls.Paging{
			Page:  1,
			Limit: 50,
			Total: total,
		},
	}, nil
}
